package com.iuie.ns.system.utils;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.iuie.ns.frame.utils.StringUtils;
import com.iuie.ns.system.vo.code.SysCodeInfoVO;
import com.iuie.ns.system.vo.code.SysCodeTypeVO;

/**
 * 将码表转成文本
 * @author liujie
 * @date 2018年5月28日下午9:49:38
 */
public class CodeTextUtils {
	
	private CodeTextUtils() {
		
	}
	
	/**
	 * codeType to text
	 * @param codeType
	 * @return
	 * @date 2018年5月28日下午9:57:12
	 */
	public static String toText(SysCodeTypeVO codeType) {
		if (codeType == null) {
			return null;
		}
		StringBuilder sb = new StringBuilder(12);
		sb.append("name: ")
			.append(codeType.getCodeTypeName())
			.append(", type: ")
			.append(codeType.getCodeTypeId());
		
		Set<SysCodeInfoVO> codeInfos = codeType.getCodeInfos();
		if (codeInfos == null || codeInfos.size() <= 0) {
			return sb.toString();
		}
		Iterator<SysCodeInfoVO> it = codeInfos.iterator();
		while (it.hasNext()) {
			SysCodeInfoVO codeInfo = it.next();
			if (codeInfo.getState() == -1) {
				continue ;
			}
			sb.append("\n")
				.append("name: ")
				.append(codeInfo.getCodeInfoName())
				.append(", value: ")
				.append(codeInfo.getCodeInfoValue());
			if (StringUtils.isBlank(codeInfo.getExt1())) {
				continue;
			}
			sb.append(", ext: ")
				.append(codeInfo.getExt1());
		}
		return sb.toString();
	}
	
	/**
	 * list to text
	 * @param types
	 * @return
	 * @date 2018年5月28日下午10:01:09
	 */
	public static String toText(List<SysCodeTypeVO> types) {
		if (types == null || types.size() <= 0) {
			return null;
		}
		StringBuilder sb = new StringBuilder(32);
		Iterator<SysCodeTypeVO> it = types.iterator();
		while (it.hasNext()) {
			SysCodeTypeVO codeType = it.next();
			String s = CodeTextUtils.toText(codeType);
			sb.append(s);
			if (it.hasNext()) {
				sb.append("\n\n");
			}
		}
		return sb.toString();
	}

}
